use :: c2rust_bitfields;

extern "C" {
    pub type re_dfa_t;
    fn regcomp(__preg: *mut regex_t, __pattern: *const i8, __cflags: i32) -> i32;
    fn regexec(
        __preg: *const regex_t,
        __String: *const i8,
        __nmatch: size_t,
        __pmatch: *mut regmatch_t,
        __eflags: i32,
    ) -> i32;
    fn regfree(__preg: *mut regex_t);
    fn memcpy(_: *mut (), _: *const (), _: u64) -> *mut ();
    fn strlen(_: *const i8) -> u64;
    fn xrealloc(_: *mut (), _: size_t) -> *mut ();
    fn xstrdup(_: *const i8) -> *mut i8;
}
pub type __u_int = u32;
pub type __ssize_t = i64;
pub type u_int = __u_int;
pub type ssize_t = __ssize_t;
pub type size_t = u64;
pub type __re_long_size_t = u64;
pub type reg_syntax_t = u64;
#[derive(Copy, Clone, BitfieldStruct)]
#[repr(C)]
pub struct re_pattern_buffer {
    pub buffer: *mut re_dfa_t,
    pub allocated: __re_long_size_t,
    pub used: __re_long_size_t,
    pub syntax: reg_syntax_t,
    pub fastmap: *mut i8,
    pub translate: *mut u8,
    pub re_nsub: size_t,
    #[bitfield(name = "can_be_null", ty = "u32", bits = "0..=0")]
    #[bitfield(name = "regs_allocated", ty = "u32", bits = "1..=2")]
    #[bitfield(name = "fastmap_accurate", ty = "u32", bits = "3..=3")]
    #[bitfield(name = "no_sub", ty = "u32", bits = "4..=4")]
    #[bitfield(name = "not_bol", ty = "u32", bits = "5..=5")]
    #[bitfield(name = "not_eol", ty = "u32", bits = "6..=6")]
    #[bitfield(name = "newline_anchor", ty = "u32", bits = "7..=7")]
    pub can_be_null_regs_allocated_fastmap_accurate_no_sub_not_bol_not_eol_newline_anchor: [u8; 1],
    #[bitfield(padding)]
    pub c2rust_padding: [u8; 7],
}
pub type regex_t = re_pattern_buffer;
pub type regoff_t = i32;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct regmatch_t {
    pub rm_so: regoff_t,
    pub rm_eo: regoff_t,
}
extern "C" fn regsub_copy(
    mut buf: *mut *mut i8,
    mut len: *mut ssize_t,
    mut text: *const i8,
    mut start: size_t,
    mut end: size_t,
) {
    let mut add: size_t = end.wrapping_sub(start);
    (unsafe {
        *buf = xrealloc(
            *buf as *mut (),
            (*len as u64)
                .wrapping_add(add)
                .wrapping_add(1 as i32 as u64),
        ) as *mut i8
    });
    (unsafe {
        memcpy(
            (*buf).offset(*len as isize) as *mut (),
            text.offset(start as isize) as *const (),
            add,
        )
    });
    (unsafe { *len = (*len as u64).wrapping_add(add) as ssize_t as ssize_t });
}
extern "C" fn regsub_expand(
    mut buf: *mut *mut i8,
    mut len: *mut ssize_t,
    mut with: *const i8,
    mut text: *const i8,
    mut m: *mut regmatch_t,
    mut n: u_int,
) {
    let mut cp: *const i8 = std::ptr::null::<i8>();
    let mut i: u_int = 0;
    let mut current_block_5: u64;
    cp = with;
    while (unsafe { *cp }) as i32 != '\0' as i32 {
        if (unsafe { *cp }) as i32 == '\\' as i32 {
            cp = unsafe { cp.offset(1) };
            if (unsafe { *cp }) as i32 >= '0' as i32 && (unsafe { *cp }) as i32 <= '9' as i32 {
                i = ((unsafe { *cp }) as i32 - '0' as i32) as u_int;
                if i < n
                    && (unsafe { (*m.offset(i as isize)).rm_so })
                        != (unsafe { (*m.offset(i as isize)).rm_eo })
                {
                    regsub_copy(
                        buf,
                        len,
                        text,
                        (unsafe { (*m.offset(i as isize)).rm_so }) as size_t,
                        (unsafe { (*m.offset(i as isize)).rm_eo }) as size_t,
                    );
                    current_block_5 = 16668937799742929182;
                } else {
                    current_block_5 = 17216689946888361452;
                }
            } else {
                current_block_5 = 17216689946888361452;
            }
        } else {
            current_block_5 = 17216689946888361452;
        }
        match current_block_5 {
            17216689946888361452 => {
                (unsafe {
                    *buf = xrealloc(
                        *buf as *mut (),
                        (*len + 2 as i64) as size_t,
                    ) as *mut i8
                });
                let fresh0 = unsafe { *len };
                (unsafe { *len = *len + 1 });
                (unsafe { *(*buf).offset(fresh0 as isize) = *cp });
            }
            _ => {}
        }
        cp = unsafe { cp.offset(1) };
    }
}
#[no_mangle]
pub extern "C" fn regsub(
    mut pattern: *const i8,
    mut with: *const i8,
    mut text: *const i8,
    mut flags: i32,
) -> *mut i8 {
    let mut r: regex_t = regex_t {
        buffer: std::ptr::null_mut::<re_dfa_t>(),
        allocated: 0,
        used: 0,
        syntax: 0,
        fastmap: std::ptr::null_mut::<i8>(),
        translate: std::ptr::null_mut::<u8>(),
        re_nsub: 0,
        can_be_null_regs_allocated_fastmap_accurate_no_sub_not_bol_not_eol_newline_anchor: [0; 1],
        c2rust_padding: [0; 7],
    };
    let mut m: [regmatch_t; 10] = [regmatch_t { rm_so: 0, rm_eo: 0 }; 10];
    let mut start: ssize_t = 0;
    let mut end: ssize_t = 0;
    let mut last: ssize_t = 0;
    let mut len: ssize_t = 0 as ssize_t;
    let mut empty: i32 = 0;
    let mut buf: *mut i8 = std::ptr::null_mut::<i8>();
    if (unsafe { *text }) as i32 == '\0' as i32 {
        return unsafe { xstrdup(b"\0" as *const u8 as *const i8) };
    }
    if (unsafe { regcomp(&mut r, pattern, flags) }) != 0 {
        return std::ptr::null_mut::<i8>();
    }
    start = 0 as ssize_t;
    last = 0 as ssize_t;
    end = (unsafe { strlen(text) }) as ssize_t;
    while start <= end {
        if (unsafe {
            regexec(
                &mut r,
                text.offset(start as isize),
                (::core::mem::size_of::<[regmatch_t; 10]>() as u64)
                    .wrapping_div(::core::mem::size_of::<regmatch_t>() as u64),
                m.as_mut_ptr(), 0,
            )
        }) != 0
        {
            regsub_copy(&mut buf, &mut len, text, start as size_t, end as size_t);
            break;
        } else {
            regsub_copy(
                &mut buf,
                &mut len,
                text,
                last as size_t,
                (m[0 as i32 as usize].rm_so as i64 + start) as size_t,
            );
            if empty != 0
                || start + m[0 as i32 as usize].rm_so as i64 != last
                || m[0 as i32 as usize].rm_so != m[0 as i32 as usize].rm_eo
            {
                regsub_expand(
                    &mut buf,
                    &mut len,
                    with,
                    unsafe { text.offset(start as isize) },
                    m.as_mut_ptr(),
                    (::core::mem::size_of::<[regmatch_t; 10]>() as u64)
                        .wrapping_div(::core::mem::size_of::<regmatch_t>() as u64)
                        as u_int,
                );
                last = start + m[0 as i32 as usize].rm_eo as i64;
                start += m[0 as i32 as usize].rm_eo as i64;
                empty = 0;
            } else {
                last = start + m[0 as i32 as usize].rm_eo as i64;
                start += (m[0 as i32 as usize].rm_eo + 1) as i64;
                empty = 1;
            }
            if (unsafe { *pattern }) as i32 != '^' as i32 {
                continue;
            }
            regsub_copy(&mut buf, &mut len, text, start as size_t, end as size_t);
            break;
        }
    }
    (unsafe { *buf.offset(len as isize) = '\0' as i32 as i8 });
    (unsafe { regfree(&mut r) });
    return buf;
}
